/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.amse.grigory.dchess.io;

import java.io.File;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.amse.grigory.dchess.kernel.IModel;

/**
 *
 * @author grigory
 */
public class GameLoader {
    private Document myDom;

    public void loadGame(IModel model,File file) throws Exception{
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        myDom = null;
        DocumentBuilder db = dbf.newDocumentBuilder();
        myDom = db.parse(file);
        Node node=myDom.getChildNodes().item(0);
        int n=node.getChildNodes().getLength();
        for (int i=0; i<n; i++){
            String s=node.getChildNodes().item(i).getNodeName();
            if (s.equals("rules")){
                loadRules(model,node.getChildNodes().item(i));
            }
            if (s.equals("time")){
                loadTime(model,node.getChildNodes().item(i));
            }
            if (s.equals("moves")){
                loadMoves(model,node.getChildNodes().item(i));
            }
            
        }
        
    }
    private void loadRules(IModel model,Node node) throws Exception{
        String s=node.getAttributes().item(0).getNodeValue();
        model.setRulesFile(s);       
        model.newGame();
        model.start();
    }
    private void loadTime(IModel model,Node node){
        int n=node.getAttributes().getLength();
        for (int i=0; i<n; i++){
            Node tnode=node.getAttributes().item(i);
            String s=tnode.getNodeName();
            int j=Integer.valueOf(tnode.getNodeValue());
            if (s.equals("black")){
                model.setBlackTime(j);
            }
            if (s.equals("white")){
                model.setWhiteTime(j);
            }
        }
    }
    private void loadMoves(IModel model,Node node){
        int n=node.getChildNodes().getLength();        
        for (int i=0; i<n; i++){
            Node tnode=node.getChildNodes().item(i);
            if (!tnode.getNodeName().equals("move")){
                continue;
            }
            int m=tnode.getAttributes().getLength();            
            int sx = 0,sy = 0,ex = 0,ey = 0;
            for (int j=0; j<m; j++){
                if (tnode.getAttributes().item(j).getNodeName().equals("sx")){
                    sx=Integer.valueOf(tnode.getAttributes().item(j).getNodeValue());
                }
                if (tnode.getAttributes().item(j).getNodeName().equals("sy")){
                    sy=Integer.valueOf(tnode.getAttributes().item(j).getNodeValue());
                }
                if (tnode.getAttributes().item(j).getNodeName().equals("ex")){
                    ex=Integer.valueOf(tnode.getAttributes().item(j).getNodeValue());
                }
                if (tnode.getAttributes().item(j).getNodeName().equals("ey")){
                    ey=Integer.valueOf(tnode.getAttributes().item(j).getNodeValue());
                }
            }            
            model.makeMove(sx, sy, ex, ey);
            
        }
    }
}
